﻿var play = true;
// Delay between animations in milliseconds
var animationDelay = "5000";
//Delay during fadein/fadeout
var fadeDelay = "1000";
var timeout = setTimeout(animationDelay);
$(document).ready(function () {
	$("#centerpiece .story").hide();
	$("#centerpiece .story").first().show();
	$("#centerpiece .controls li").first().addClass("active");
	ShowCenterpiece(1);
	var controls = $("#centerpiece .controls li a"); 
    controls.click(function (e) {
        e.preventDefault();
		var currentIndex = controls.index($(this)) + 1;
		// Set this to true if you want the click to stop the animation
		//play = false;
		ShowCenterpiece(currentIndex);
    });
});
function Animate() {
	var num = $(".story").index($(".story:visible")) + 1;
    if (play) {
		clearTimeout(timeout);
		timeout = setTimeout(function () {
            if (num >= $("#centerpiece .story").length) num = 0;
            ShowCenterpiece(num + 1);
        }, animationDelay);
	}
}
function ShowCenterpiece(num) {
    var pos = (num - 1);
    $("#centerpiece .controls li").removeClass("active");
	$("#centerpiece .controls li:eq(" + pos + ")").addClass("active");
	$("#centerpiece .story").fadeOut(fadeDelay);
    $("#centerpiece .story:eq(" + pos + ")").fadeIn(fadeDelay,function(){	
		Animate();
	});
}